package com.medical.admin.hospital.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.medical.admin.cost.mapper.DepartmentMapper;
import com.medical.admin.hospital.common.dto.DepartmentDto;
import com.medical.admin.hospital.common.vo.ComboVo;
import com.medical.admin.hospital.common.vo.DepartmentVo;
import com.medical.admin.hospital.common.vo.PageVo;
import com.medical.admin.hospital.entity.*;
import com.medical.admin.hospital.mapper.HospitalMapper;
import com.medical.admin.hospital.service.DepartmentService;
import com.qf.medical.ex.BaseException;
import com.qf.medical.response.ResponseCode;
import com.qf.medical.utils.BeanConvertUtils;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Service
public class DepartServiceImpl implements DepartmentService {
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private HospitalMapper hospitalMapper;

    /**
     * 下拉展示科室名称，默认父级目录为1
     * @param parentId
     * @return
     */
    @Override
    public DepartmentVo showDepartments(Long parentId) {
        return departmentMapper.selectByParentId(parentId);
    }

    /**
     * 动态分页查询科室信息
     * @param departmentName
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public PageVo<DepartmentVo> list(String departmentName, int pageNum, int pageSize) {
        PageVo<DepartmentVo> pageVo = new PageVo<>();
        List<DepartmentVo> departments= departmentMapper.pageDepartments(departmentName,(pageNum-1)*pageSize,pageSize);
        pageVo.setRows(BeanConvertUtils.convertListTo(departments,DepartmentVo::new));
        pageVo.setTotal(departments.size());
        return pageVo;
    }

    /**
     * 新增科室信息
     * @param departmentDto
     * @return
     */
    @Override
    public Integer insert(DepartmentDto departmentDto) {
        //通过科室名称判断科室是否存在
        com.medical.admin.cost.entity.Department department = departmentMapper.findByDepartmentName(departmentDto.getDepartmentName());
        if (ObjectUtil.isNotEmpty(department)){
            throw new BaseException(ResponseCode.DEPARTMENT_EXIT_ERROR);
        }

        //通过上级名称获取parentId
        Department parent =  departmentMapper.findByParentName(departmentDto.getParentName());
        //转换对象
        Department dt  = BeanConvertUtils.convertTo(departmentDto,Department ::new);

        //通过医院名称查询id
        Hospital hospital=hospitalMapper.findByHospitalName(departmentDto.getHospitalName());
        dt.setHospitalId(hospital.getHospitalId());

        //通过上级科室名称获取id存放到二级id中
        dt.setParentId(parent.getDepartmentId());
        com.medical.admin.cost.entity.Department costDepartment = new com.medical.admin.cost.entity.Department();
        BeanUtil.copyProperties(dt,costDepartment);

        //新增科室信息
        int count = departmentMapper.insert(costDepartment);
        if (count!=1) {
            throw new BaseException(ResponseCode.SYS_ERROR);
        }
        return count;
    }

    /**
     * 修改科室信息
     * @param departmentDto
     * @return
     */
    @Override
    public Integer update(DepartmentDto departmentDto) {
        //通过科室名称判断科室是否存在
        com.medical.admin.cost.entity.Department department = departmentMapper.findByDepartmentName(departmentDto.getDepartmentName());
        if (ObjectUtil.isNotEmpty(department)){
            throw new BaseException(ResponseCode.DEPARTMENT_EXIT_ERROR);
        }

        //通过上级名称获取parentId
        Department parent =  departmentMapper.findByParentName(departmentDto.getParentName());

        //转换对象
        Department dt  = BeanConvertUtils.convertTo(departmentDto,Department ::new);

        //通过医院名称查询id
        Hospital hospital=hospitalMapper.findByHospitalName(departmentDto.getHospitalName());
        hospital.setHospitalId(hospital.getHospitalId());

        //通过上级科室名称获取id存放到二级id中
        dt.setParentId(parent.getDepartmentId());
        com.medical.admin.cost.entity.Department costDepartment = new com.medical.admin.cost.entity.Department();
        BeanUtil.copyProperties(dt,costDepartment);

        //修改科室信息
        int count = departmentMapper.updateById(costDepartment);
        if (count!=1) {
            throw new BaseException(ResponseCode.SYS_ERROR);
        }
        return count;
    }

    /**
     * 修改科室信息
     * @param departmentId
     * @return
     */
    @Override
    public Integer delete(Long departmentId) {
        com.medical.admin.cost.entity.Department department = departmentMapper.selectById(departmentId);
        //判断套餐是否存在
        Optional.ofNullable(department).orElseThrow(()->new BaseException(ResponseCode.DEPARTMENT_NOT_EXIT_ERROR));
        //判断套餐是否已删除
        if (department.getState()!=1){
            throw new BaseException(ResponseCode.DEPARTMENT_NOT_EXIT_ERROR);
        }
        department.setState(0);
        int count = departmentMapper.updateById(department);
        if (count!=1){
            throw new BaseException(ResponseCode.SYS_ERROR);
        }
        return count;
    }

    /**
     * 查询科室信息详情
     * @param departmentId
     * @return
     */
    @Override
    public DepartmentVo select(Long departmentId) {
        com.medical.admin.cost.entity.Department department = departmentMapper.selectById(departmentId);
        Optional.ofNullable(department).orElseThrow(()-> new BaseException(ResponseCode.DEPARTMENT_NOT_EXIT_ERROR));
        if (department.getState()!=1){
            throw new BaseException(ResponseCode.DEPARTMENT_NOT_EXIT_ERROR);
        }
        return BeanConvertUtils.convertTo(department, DepartmentVo::new);
    }

    /**
     * 展示科室一级目录信息
     * @return
     */
    @Override
    public List<DepartmentVo> showParent() {
        LambdaQueryWrapper<com.medical.admin.cost.entity.Department> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(com.medical.admin.cost.entity.Department::getParentId,0);
        List<com.medical.admin.cost.entity.Department> department = departmentMapper.selectList(queryWrapper);
        return BeanConvertUtils.convertListTo(department,DepartmentVo::new);
    }
}
